package com.whfc.cncit.helmet.processor;

import com.whfc.cncit.constant.CncitConst;
import com.whfc.cncit.entity.CncitDwMsgPS;
import com.whfc.cncit.entity.CncitDwMsgRA;
import com.whfc.cncit.entity.CncitUpMsgR;
import com.whfc.emp.enums.HelmetType;
import com.whfc.iot.session.Session;
import com.whfc.iot.session.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.whfc.cncit.constant.CncitConst.SUCCESS;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/26 11:37
 */
@Component
public class HelmetUpMsgProcessorR implements CncitUpMsgProcessor<CncitUpMsgR> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SessionManager sessionManager;

    private static final String TTS_CONTENT = "欢迎使用智能安全帽";

    @Override
    public void processMsg(CncitUpMsgR msg) {
        logger.info("处理安全帽开机命令:{}", msg.toString());
        try {
            String deviceCode = msg.getDeviceCode();
            Integer deviceId = msg.getDeviceId();
            Session session = sessionManager.getSessionByDeviceId(deviceId);
            CncitDwMsgRA retMsg = new CncitDwMsgRA(deviceCode, SUCCESS);
            session.getChannel().writeAndFlush(retMsg).addListener(future -> {
                logger.info("RA发送结果,deviceCode={},isDone={},isSuccess={}", deviceCode, future.isDone(), future.isSuccess());
            });

            // 发送广播
            if (!String.valueOf(HelmetType.VOICE.getValue()).equals(msg.getExt1())) {
                return;
            }
            CncitDwMsgPS cncitDwMsgPS = new CncitDwMsgPS();
            cncitDwMsgPS.setType(CncitConst.CMD_TTSD);
            cncitDwMsgPS.setParam(TTS_CONTENT);
            cncitDwMsgPS.setDeviceCode(deviceCode);
            session.getChannel().writeAndFlush(cncitDwMsgPS);
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Integer(1).equals(Integer.valueOf("1")));
    }
}
